// eslint-disable-next-line @typescript-eslint/no-explicit-any
interface IPAGE_CONFIG {
  code: string
}

const injectScript = (pageConfig: IPAGE_CONFIG) => {
  const el = document.createElement('script')
  el.type = 'text/javascript'
  el.textContent = pageConfig.code
  document.body.appendChild(el)
}

const pageRead = () => {
  chrome.runtime.sendMessage(
    {
      type: 'get_page_modifier_config',
      params: {
        url: window.location.href
      }
    },
    (pageConfig?: IPAGE_CONFIG) => {
      if (pageConfig) {
        injectScript(pageConfig)
      }
    }
  )
}

pageRead()
